﻿<?php 
if (!defined('WEB_ROOT')) {
	exit;
}

$psubcat   = (isset($_GET['subcat']) && $_GET['subcat'] != '') ? $_GET['subcat'] : '';
$tagName = (isset($_GET['tag']) && $_GET['tag'] != '') ? $_GET['tag'] : '';
$catTitle = getTitle($pcat, $psubcat);
switch ($pcat) {
	case 'gttl':
		$sql = "SELECT sh.id, sh.masach, sh.tensach, sh.namxb, sh.nhaxb,
			sh.hinhanh, sh.hinhanh_thumb, 
			GROUP_CONCAT(DISTINCT hotentg) cac_tg,
			GROUP_CONCAT(DISTINCT tg.id) as cac_tgid 
		FROM `sach` sh JOIN tacgia_sach ts ON sh.id = ts.sach_id
			JOIN tacgia tg ON ts.tg_id = tg.id
		GROUP BY sh.id
		ORDER BY sh.ngay_dang";
		break;
	case 'hpsp':
		$sql = " SELECT hs.id, hs.tenhpsp, hs.mahpsp, hs.tentrienlam, hs.ngaytrienlam, hs.pd_image, hs.pd_thumbnail, hs.giaithuong, hs.ngay_dang,
					GROUP_CONCAT(DISTINCT hotentg) cac_tg,
					GROUP_CONCAT(DISTINCT tg.id) as cac_tgid 
		FROM hoapham_sanpham hs
			JOIN tacgia_hspham tghs ON hs.id = tghs.hsp_id
			JOIN tacgia tg ON tghs.tg_id = tg.id
		GROUP BY hs.id 
		ORDER BY hs.ngay_dang ";
		break;
	default:
		break;
}

$tomtat = '';
$numOfItems = NUM_PRODUCT_ITEMS_PER_MAIN_PAGE;
if ($pview != '') {
	$numOfItems = NUM_PRODUCT_ITEMS_PER_PAGE;
}
$result = dbQuery(getPagingQuery($sql, $numOfItems));
if ($psubcat == '') {
	$pagingLink = getPagingLink($sql, $numOfItems, "view=list&cat=$pcat");
} else {
	$pagingLink = getPagingLink($sql, $numOfItems, "view=list&cat=$pcat&subcat=$psubcat");
}
$count = dbNumRows($result);
//echo $count;
//var_dump($result);
?>

<div id="cat_<?php echo $pcat."_".$psubcat; ?>" class="cat-widget">
	<h3>
		<a title="" href="javascript:<?php if ($psubcat != '') echo "viewList('".$pcat."','".$catTitle."','".$psubcat."');"; else echo "viewTag('".$pcat."','".$catTitle."','".$tagName."');"; ?>"><?php echo $catTitle; ?></a>
		<a class="minimize"></a>
	</h3>
	<div class="container">
		<div class="mpost">
			<ul style="display:block;">
			<?php 
			for ($rowIdx = 0; $rowIdx < $count  ; $rowIdx++) {
				 ?>
				
				<?php 
					$row = dbFetchAssoc($result);
					$thumbnail = "images/common/article_default.jpg";
					if ($pcat == 'gttl') {
						$thumbnail = "images/book/book_default.jpg";
						if (isset($row['hinhanh_thumb']) && ($row['hinhanh_thumb'] != '')) {
							$thumbnail = "images/book/".$row['hinhanh_thumb'];
						}
					} else if ($pcat == 'hpsp') {
						$thumbnail = "images/product/product_default.jpg";
						if (isset($row['pd_thumbnail']) && ($row['pd_thumbnail'] != '')) {
							$thumbnail = "images/product/".$row['pd_thumbnail'];
						}
					}
				 ?>
					<li style="list-style-type: none;display:inline; padding-right:25px;float:left;width:45%;margin-bottom:10px;">
						<a href="#" style="float:left;margin-right:10px;">
							<img src="<? echo $thumbnail; ?>" width="100" height="100" style="margin-bottom:10px;"></img>
						</a>
						<div id="sach_info" style="display:inline;">
							<ul>
							<?php if ($pcat == 'gttl') { ?>
								<li><b><?php echo $row['tensach']; ?></b></li>
								<li>Mã sách: <?php echo $row['masach'];?></li>
								<li>Tác giả: <?php echo $row['cac_tg'];?></li>
								<li>NXB: <?php echo $row['nhaxb'];?></li>
								<li>Năm: <?php echo $row['namxb'];?></li>
							<?php } else { ?>
								<li><b><?php echo $row['tenhpsp']; ?></b></li>
								<li>Mã sp: <?php echo $row['mahpsp'];?></li>
								<li>Tác giả: <?php echo $row['cac_tg'];?></li>
								<li><?php echo $row['tentrienlam'];?></li>
								<li>Ngày: <?php echo $row['ngaytrienlam'];?></li>
							<?php }?>
							</ul>
						</div>
					</li>
				<?php 
				if ($rowIdx % 2 == 1) {
					echo '<div class="clear" />';
				}
				} ?>
			</ul>
	
			
			<?php if ($pagingLink != '' && $pview != '') { ?>
			<div class="pagination-bar">
				<div class="pagination"><?php echo $pagingLink; ?></div>
				<div class="clear"></div>
			</div>
			<?php } ?>
		</div>
	</div>
	<div class="footer"></div>
	<div class="clear"></div>
</div>
